<?php
declare (strict_types = 1);

namespace app\shop\controller;

use think\Request;
use think\facade\Session;

class Manager
{
    //

    /**
     * 登录
     */
    public function doLogin(Request $request){
//        password_encrypt 定义的公共函数
//        dd(password_encrypt(678910));
        $username = $request->post('username');
        $password = $request->post('password');
        $captcha = $request->post('code');
        # 验证验证码
//        if(!captcha_check($captcha)){
//            // 验证失败
//            return json(['code' => 2001,'msg' =>'验证码不正确','data' => []]);
//        }

        $info = \app\shop\model\Manager::where('username',$username)->find();

        if (empty($info)) {
//            return xml(['code' => 2001,'msg' =>'用户名不存在','data' => []]); // xml格式
            return json(['code' => 2001,'msg' =>'用户名不存在','data' => []]);
        }

        // 按照相同的加密方法进行对比

        if ($info->password != password_encrypt($password)) {
            return json(['code' => 2001,'msg' =>'密码错误','data' => []]);
        }

        // 登录成功之后、利用会话控制技术
        Session::set('username',$info->username);

        return json(['code' => 200,'msg' =>'登录成功','data' => []]);
    }
}
